{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "64971178-177d-48ff-9c11-201f3306a6b6",
   "metadata": {
    "tags": []
   },
   "source": [
    "# 10Q Analysis\n",
    "In this demo, we explore answering complex queries by decomposing them into simpler sub-queries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3edf4c98-de44-4d0b-ac7a-7273255a21cf",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ae95482c-516d-41ce-8394-db949ed58716",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from llama_index import SimpleDirectoryReader, ServiceContext, VectorStoreIndex\n",
    "from llama_index.response.pprint_utils import pprint_response\n",
    "from llama_index.llms import OpenAI\n",
    "\n",
    "from llama_index.tools import QueryEngineTool, ToolMetadata\n",
    "from llama_index.query_engine import SubQuestionQueryEngine"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f2ffdb6c-b7df-4649-84d5-2b4ce32ad8a9",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Configure LLM service"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "96edd29c-15b6-44f9-bae0-8a1f97e07d17",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "llm = OpenAI(temperature=0, model=\"text-davinci-003\", max_tokens=-1)\n",
    "service_context = ServiceContext.from_defaults(llm=llm)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e034ed70-9d74-4ded-9797-13f2cc5c5ac8",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "133fc28d-65a1-4a1f-8ed0-d6fb3d4e160c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "march_2022 = SimpleDirectoryReader(\n",
    "    input_files=[\"../data/10q/uber_10q_march_2022.pdf\"]\n",
    ").load_data()\n",
    "june_2022 = SimpleDirectoryReader(\n",
    "    input_files=[\"../data/10q/uber_10q_june_2022.pdf\"]\n",
    ").load_data()\n",
    "sept_2022 = SimpleDirectoryReader(\n",
    "    input_files=[\"../data/10q/uber_10q_sept_2022.pdf\"]\n",
    ").load_data()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "210ec44c-f559-48cb-9217-1efaca5e5362",
   "metadata": {},
   "source": [
    "# Build indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3748ac62-42ab-4f4c-b40b-40313a44b80d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "march_index = VectorStoreIndex.from_documents(march_2022)\n",
    "june_index = VectorStoreIndex.from_documents(june_2022)\n",
    "sept_index = VectorStoreIndex.from_documents(sept_2022)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "04fd6587-db6d-4782-83ad-ffc2fcd0b3a1",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Build query engines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a2e4e609-fa2c-432f-ab93-c1607be350a1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "march_engine = march_index.as_query_engine(similarity_top_k=3)\n",
    "june_engine = june_index.as_query_engine(similarity_top_k=3)\n",
    "sept_engine = sept_index.as_query_engine(similarity_top_k=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5fccbd2c-e43f-4ec6-98d3-bdc3d97bc6cc",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "query_engine_tools = [\n",
    "    QueryEngineTool(\n",
    "        query_engine=sept_engine,\n",
    "        metadata=ToolMetadata(\n",
    "            name=\"sept_22\",\n",
    "            description=\"Provides information about Uber quarterly financials ending September 2022\",\n",
    "        ),\n",
    "    ),\n",
    "    QueryEngineTool(\n",
    "        query_engine=june_engine,\n",
    "        metadata=ToolMetadata(\n",
    "            name=\"june_22\",\n",
    "            description=\"Provides information about Uber quarterly financials ending June 2022\",\n",
    "        ),\n",
    "    ),\n",
    "    QueryEngineTool(\n",
    "        query_engine=march_engine,\n",
    "        metadata=ToolMetadata(\n",
    "            name=\"march_22\",\n",
    "            description=\"Provides information about Uber quarterly financials ending March 2022\",\n",
    "        ),\n",
    "    ),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3b63f755-dda4-49c8-8a00-917d3687e2c1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "s_engine = SubQuestionQueryEngine.from_defaults(query_engine_tools=query_engine_tools)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0888e0b1-bcc8-4ea0-8162-f4a3eea97475",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Run queries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "d0e45eb4-39f5-459f-8d57-89cf4d80bc00",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generated 2 sub questions.\n",
      "\u001b[36;1m\u001b[1;3m[sept_22] Q: What is the revenue growth of Uber for the quarter ending September 2022\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m[sept_22] A: compared to the same period in 2021?\n",
      "\n",
      "The revenue growth of Uber for the quarter ending September 2022 compared to the same period in 2021 is 72%.\n",
      "\u001b[0m\u001b[33;1m\u001b[1;3m[june_22] Q: What is the revenue growth of Uber for the quarter ending June 2022\n",
      "\u001b[0m\u001b[33;1m\u001b[1;3m[june_22] A: compared to the same period in 2021?\n",
      "\n",
      "The revenue growth of Uber for the quarter ending June 2022 compared to the same period in 2021 is 105%.\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "response = s_engine.query(\n",
    "    \"Analyze Uber revenue growth over the latest two quarter filings\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "55c67ff8-7a7f-4032-bef8-373f0c272fd2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Uber's revenue growth over the latest two quarter filings has been strong, with a 72% increase for the quarter ending September 2022 compared to the same period in 2021, and a 105% increase for the quarter ending June 2022 compared to the same period in 2021.\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0ff55b9b-2b1f-4921-bd5d-5ab04b957b30",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generated 3 sub questions.\n",
      "\u001b[36;1m\u001b[1;3m[sept_22] Q: What is the macro environment in September 2022\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m[sept_22] A: \n",
      "The macro environment in September 2022 is one of recovery from the impacts of the COVID-19 pandemic, with increases in Mobility Trip volumes, a $1.3 billion increase in Freight Gross Bookings resulting from the acquisition of Transplace, a $1.1 billion increase in Mobility revenue due to business model changes in the UK, and a $164 million increase in Delivery revenue due to an increase in certain Courier payments and incentives. Additionally, there was a $2.2 billion net increase in Mobility revenue due to business model changes in the UK and an accrual made for the resolution of historical claims in the UK relating to the classification of drivers, and a $751 million increase in Delivery revenue due to an increase in certain Courier payments and incentives.\n",
      "\u001b[0m\u001b[33;1m\u001b[1;3m[june_22] Q: What is the macro environment in June 2022\n",
      "\u001b[0m\u001b[33;1m\u001b[1;3m[june_22] A: \n",
      "In June 2022, the macro environment is characterized by the continued impact of COVID-19 restrictions on global demand, the adoption of new accounting standards, and the potential for shifts in consumer travel patterns due to health concerns.\n",
      "\u001b[0m\u001b[38;5;200m\u001b[1;3m[march_22] Q: What is the macro environment in March 2022\n",
      "\u001b[0m\u001b[38;5;200m\u001b[1;3m[march_22] A: \n",
      "The macro environment in March 2022 is uncertain, as the effects of the COVID-19 pandemic and the actions taken to mitigate it are still being felt. Travel restrictions, business restrictions, school closures, and limitations on social or public gatherings may still be in place in some regions, and the demand for services may still be affected.\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "response = s_engine.query(\"Analyze change in macro environment over the 3 quarters\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "feccbe75-0005-40de-93aa-9ed7c192dd50",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The macro environment has seen a significant change over the three quarters from March 2022 to September 2022. In March 2022, the macro environment was uncertain due to the effects of the COVID-19 pandemic and the actions taken to mitigate it. By June 2022, the macro environment was characterized by the continued impact of COVID-19 restrictions on global demand, the adoption of new accounting standards, and the potential for shifts in consumer travel patterns due to health concerns. By September 2022, the macro environment had shifted to one of recovery from the impacts of the COVID-19 pandemic, with increases in Mobility Trip volumes, a $1.3 billion increase in Freight Gross Bookings resulting from the acquisition of Transplace, a $1.1 billion increase in Mobility revenue due to business model changes in the UK, and a $164 million increase in Delivery revenue due to an increase in certain Courier payments and incentives. Additionally, there was a $2.2 billion net increase in Mobility revenue due to business model changes in the UK and an accrual made for the resolution of historical claims in the UK relating to the classification of drivers, and a $751 million increase in Delivery revenue due to an increase in certain Courier payments and incentives.\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "a20ac08f-5859-4433-9ed8-5c315a2e050b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generated 1 sub questions.\n",
      "\u001b[36;1m\u001b[1;3m[sept_22] Q: How much cash did Uber have in September 2022\n",
      "\u001b[0m\u001b[36;1m\u001b[1;3m[sept_22] A: \n",
      "Uber had $4,865 million in cash in September 2022.\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "response = s_engine.query(\"How much cash did Uber have in sept 2022\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "cf67b5f4-a470-4deb-b2d8-fe752e86e170",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Uber had $4,865 million in cash in September 2022.\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
